var spareId = getQueryString("spareId");
var producor = getQueryString("producor");
var devType = getQueryString("devType");
var Id = getQueryString("Id");
$(function () {
    var queryUrl = '../sparedev/list';
    var queryParam = '';
    if(Id){
        queryParam += '&id=' + Id;
    }
    if(spareId){
        queryParam += '&spareRepId=' + spareId;
    }
    if(queryParam.length > 0){
        queryUrl += '?' + queryParam.substring(1);
    }
    $("#jqGrid").Grid({
        url: queryUrl,
        colModel: [
            {label: '系统编码/ERP码', name: 'partCode', index: 'PART_CODE', width: 80},
            {label: '名称', name: 'name', index: 'NAME', width: 80},
            {label: '规格', name: 'spec', index: 'SPEC', width: 80},
            {label: '生产厂家', name: 'producor', index: 'PRODUCOR', width: 80},
            {label: '使用单位', name: 'useUnit', index: 'USE_UNIT', width: 80},
            {label: 'id', name: 'id', key: true, hidden: true},
            {name: 'checkFlag', hidden: true},
            {
                label: '审核状态', index: 'CHECK_FLAG', width: 80, formatter: function (value, options, row) {
                    return sysEnums.CHECK_FLAG[row.checkFlag];
                }
            }
        ]
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        spareDev: {},
        ruleValidate: {
            spareId: [
                {required: true, message: '替换备件不能为空', trigger: 'blur'}
            ]
        },
        q: {
            partName: '', // 备件名称
            spec: '', // 备件规格
            partCode: '', // 备件编码
            erpCode: '', // ERP编码
            producor:'', // 供应厂家
        },
        spareList: [],
        sparePart: {}
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.spareDev = {id: '', delFlag: 1, spareRepId: spareId, checkFlag: 0};
            vm.getDropDown();
        },
        update: function (event) {
            var id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id);
        },
        saveOrUpdate: function (event) {
            var url = vm.spareDev.id == (null || '') ? "../sparedev/save" : "../sparedev/update";
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.spareDev),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }
            confirm('确定要删除选中的记录？', function () {
                vm.spareDev.delFlag = 0;
                Ajax.request({
                    url: "../sparedev/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function (r) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
            /*confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../sparedev/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });*/
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../sparedev/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.spareDev = r.spareDev;
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: vm.q,
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        reloadSearch: function () {
            vm.q = {
                name: '', // 备件名称
                spec: '', // 备件规格
                partCode: '', // 备件编码
                erpCode: '', // ERP编码
                producor:'', // 供应厂家
            };
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        getDropDown: function () {
            Ajax.request({
                url: "../sparepart/queryAll",
                async: true,
                successCallback: function (r) {
                    vm.spareList = r.list;
                }
            });
        },
        getBySpareId: function (item) {
            vm.sparePart = item;
            vm.spareDev.spareId = item.id;
            vm.spareDev.devId = item.devCode;
        },
        review: function () { //审核
            var result = getSelectedRowData("#jqGrid");
            var id = result.id;
            if (id == null) {
                return;
            }
            vm.spareDev.checkFlag=1;
            if (result.checkFlag === "0") {
                vm.getInfo(id);
                openWindow({
                    title: "确认审核",
                    area: ['350px', '250px'],
                    content: jQuery("#reviewLayer"),
                    btn: ['确定', '取消'],
                    btn1: function (index) {
                        Ajax.request({
                            url: '../sparedev/review',
                            params: {
                                'id': id,
                                'checkFlag': vm.spareDev.checkFlag
                            },
                            type: 'POST',
                            successCallback: function (res) {
                                alert('审核成功!');
                                setTimeout(function () {
                                    location.reload();
                                }, 1000);
                            }
                        });
                    }
                });
            } else {
                alert('本记录已审核!');
            }
        },
        //显示备件列表-物料编码
        getSparePart: function () {
            openWindow({
                type: 2,
                title: '<i class="fa fa-file-text-o"></i> 选择备件',
                content: '../spare/sparePartSelect.html',
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var iframeWin = window["layui-layer-iframe" + index];
                    var res = iframeWin.getSparePart();
                    if (!res) {
                        iview.Message.error("请选择一条记录");
                    } else {
                        layer.close(index);
                        vm.spareDev = {
                            spareId: res.id,
                            spareRepId: spareId,
                        };
                        Ajax.request({
                            url: "../sparedev/save",
                            params: JSON.stringify(vm.spareDev),
                            type: "POST",
                            contentType: "application/json",
                            successCallback: function (r) {
                                alert('操作成功', function (index) {
                                    vm.reload();
                                });
                            }
                        });
                    }
                }
            })
        }
    }
});
